#include<iostream>
//#include "opencv2/core/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <fstream>


using namespace std;

int main() {
    // 下载并处理图像
    // const std::string img_url = "https://s3.amazonaws.com/model-server/inputs/kitten.jpg";
    const std::string img_path = "./imagenet_cat.png"; // 假设你已经下载了图像

    cv::Mat image = cv::imread(img_path);
    
    // 检查图像是否成功读取
    if (image.empty()) {
        std::cerr << "Error: Unable to read the image." << std::endl;
        return -1;
    }

    // 获取图像的通道数
    int channels = image.channels();
    
    /*
    // 打印图像每个通道的像素值
    for (int i = 0; i < image.rows; ++i) {
        for (int j = 0; j < image.cols; ++j) {
            for (int c = 0; c < channels; ++c) {
                std::cout << "Pixel value at position (" << i << ", " << j << ") in channel " << c << ": ";
                std::cout << static_cast<int>(image.at<cv::Vec3b>(i, j)[c]) << std::endl;
            }
        }
    }*/
    std::cout << image.at<cv::Vec3b> << std::endl;


    // 720*720 
    std::cout << image.size << std::endl;
   
    cv::resize(image, image, cv::Size(224, 224));
    
    //224*224
    std::cout << image.size << std::endl;

    // 将图像数据转换为 float32
    cv::Mat img_data;
    image.convertTo(img_data, CV_32F);

    // 标准化图像数据
    cv::Scalar mean(0.485, 0.456, 0.406);
    cv::Scalar stddev(0.229, 0.224, 0.225);
    img_data = (img_data / 255 - mean) / stddev;
    
    // 224*224
    std::cout << image.size << std::endl;

    // 添加 batch 维度
    cv::Mat img_data_batched;
    std::vector<cv::Mat> channelss;
    split(img_data, channelss);
    cv::vconcat(channelss, img_data_batched);
    
    // 672*224
    std::cout << img_data_batched.size << std::endl;

    img_data_batched = img_data_batched.reshape(1, 1);

    // 打印输出转换后的数据和维度
    // std::cout << "转换后的数据：" << std::endl << img_data_batched << std::endl;
    std::cout << "数据维度：" << img_data_batched.size << std::endl;

    // 保存为 .npz（输出 imagenet_cat.npz）
    cv::FileStorage fs("imagenet_cat.npz", cv::FileStorage::WRITE);
    fs << "data" << img_data_batched;
    fs.release();
    
    return 0;
}
